/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-05-25 20:01:22
 * @LastEditTime: 2024-09-04 21:44:16
 * @FilePath: /nest_study/server/src/modules/system/access/access.controller.ts
 */
import { Controller, Get, Post, Body, HttpCode, Request, UseGuards } from '@nestjs/common';
import { AccessService } from './access.service';
import { LoginDto } from './dto/index.dto';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { getClientInfo } from 'src/common/utils/client-info';
import { AuthGuard } from '@nestjs/passport';

@ApiTags('登录/退出')
@Controller('')
export class AccessController {
  constructor(private readonly accessService: AccessService) { }

  @ApiOperation({
    summary: '用户登录'
  })
  // @ApiParam({ type: String, name: 'password', description: '密码' })
  // @ApiParam({ type: String, name: 'username', description: '用户名' })
  @ApiBody({
    type: LoginDto,
    required: true
  })
  @Post("/login")
  @HttpCode(200)
  create(@Body() user: LoginDto, @Request() req) {
    const clientInfo = getClientInfo(req);
    return this.accessService.login(user, clientInfo);
  }

  @ApiOperation({
    summary: '退出登陆',
  })
  @Post('/logout')
  @ApiBearerAuth()
  @UseGuards(AuthGuard('jwt'))
  logout(@Request() req) {
    const clientInfo = getClientInfo(req);
    const user = req.user;
    return this.accessService.logout(clientInfo, user);
  }

  @ApiOperation({
    summary: '获取验证图片',
  })
  @Get('/captchaImage')
  captchaImage() {
    return {
      captchaEnabled: false,
      code: 200,
      msg: '操作成功',
    };
  }


  @ApiOperation({
    summary: '用户信息',
  })
  @Get('/getInfo')
  async getInfo(@Request() req) {
    const user = req.user;
    return {
      msg: '操作成功',
      code: 200,
      permissions: user.permissions,
      roles: user.roles || ['admin'],
      user: user.user,
    };
  }

  @ApiOperation({
    summary: '生成数据库种子数据'
  })
  @Get('/init')
  async initSeed() {
    return this.accessService.initData()
  }

}
